package com.hmyd.hmyd_android.ui;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.common.base.Objects;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.HmWallet;
import com.hmyd.hmyd_android.dto.Page;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.listview.XListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 小惠钱包详情列表
 * @author wangk
 *
 */

@ContentView(R.layout.activity_recharge_history_list)
public class HmWalletListActivity extends BasicListActivity {
	
	@ViewInject(R.id.home_title)
	private View home_title;
	@ViewInject(R.id.home_left)
	private ImageView home_left;
	@ViewInject(R.id.home_right)
	private TextView home_right;
	@ViewInject(R.id.home_title_txt)
	private TextView home_title_txt;
	@ViewInject(R.id.list)
	private XListView list;
	@ViewInject(R.id.total)
	private TextView total;
	@ViewInject(R.id.bottom_label)
	private TextView bottom_label;
	
	private List<HmWallet> hmWalletList;
	
	private final static int REFRESH = 1;
	private final static int MORE = 2;
	
	private HmWalletListAdapter adapter;
	private long lastCurosr; // 游标
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		hmWalletList = new ArrayList<HmWallet>();
		adapter = new HmWalletListAdapter();
		home_title_txt.setText(getString(R.string.HM_wallet));
		home_title.setOnTouchListener(touch);
		home_right.setText(getString(R.string.assets_instructions));
		bottom_label.setText(getString(R.string.hm_wallet_total));
		
		getXList().setPullLoadEnable(true);
		getXList().setPullRefreshEnable(true);
		getXList().setAutoLoadEnable(false);
		getXList().setXListViewListener(this);
		getXList().setAdapter(adapter);
		getXList().setDividerHeight(0);
		
		onRequest(REFRESH, UrlConstant.XIAOHUIWALLETLIST, HttpMethod.GET, null);
	}
	
	@Override
	public XListView getXList() {
		return list;
	}
	
	
	@Override
	public void onPostSuccess(int postId, String result) {
		super.onPostSuccess(postId, result);
		Gson gson = new Gson();
		Type type =new TypeToken<Page<HmWallet>>(){}.getType();
		Page<HmWallet> wallets = gson.fromJson(result, type);
		if(wallets != null) {
			if(hmWalletList == null) {
				hmWalletList = new ArrayList<HmWallet>();
			}
			switch (postId) {
			case REFRESH:
				hmWalletList = wallets.records;
				break;
			case MORE:
				hmWalletList.addAll(wallets.records);
				break;
			default:
				break;
			}
			if(hmWalletList.size() > 0) {
				lastCurosr = hmWalletList.get(hmWalletList.size()-1).createdTime;
				total.setText(hmWalletList.get(0).remainingAmount+"");
			}
			if(wallets.hasMore) {
				getXList().setPullLoadEnable(true);
			} else {
				getXList().setPullLoadEnable(false);
			}
			adapter.notifyDataSetChanged();
		}
	}
	
	@Override
	public void onPostFailure(int postId, String msg) {
		super.onPostFailure(postId, msg);
		total.setText(getString(R.string.zero_num));
		getXList().stopLoadMore();
		getXList().stopRefresh();
	}
	
	@Override
	public void onRefresh() {
		super.onRefresh();
		onRequest(REFRESH, UrlConstant.XIAOHUIWALLETLIST, HttpMethod.GET, null);
	}
	
	@Override
	public void onLoadMore() {
		super.onLoadMore();
		RequestParams params = new RequestParams();
		params.addBodyParameter("lastCursor", String.valueOf(lastCurosr));
		onRequest(MORE, UrlConstant.XIAOHUIWALLETLIST, HttpMethod.GET, params);
	}
	
	@Override
	protected void leftButton() {
		super.leftButton();
		AppBasic.getInstance().finishCurrentActivity();
	}
	
	@Override
	protected void rightButton() {
		super.rightButton();
		String url = AppBasic.app.hmwalletLinkUrl;
		Utils.jumpURL(this, url, "小惠钱包使用说明", null, true);
	}
	
	/**
	 * 列表适配
	 * @author wangk
	 */
	private class HmWalletListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return hmWalletList.size();
		}

		@Override
		public Object getItem(int position) {
			return hmWalletList.get(position%hmWalletList.size());
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if(convertView == null) {
				viewHolder = new ViewHolder();
				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.view_hm_wallet_list_item, null);
				viewHolder.sourceType = (TextView) convertView.findViewById(R.id.sourceType); // 使用类型
				viewHolder.happenTime = (TextView) convertView.findViewById(R.id.happenTime); // 时间
				viewHolder.wallet_amount = (TextView) convertView.findViewById(R.id.wallet_amount); // 金额
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			HmWallet hmw = (HmWallet) getItem(position);
			if(Objects.equal(null, hmw)) {
				return null;
			}
			viewHolder.sourceType.setText(hmw.sourceType);
			viewHolder.happenTime.setText(Utils.getDateString(hmw.createdTime));
			viewHolder.wallet_amount.setText(hmw.amount+"");
			if((hmw.amount+"").trim().startsWith("-") || hmw.sourceType.trim().equals("投资抵用")) {
				
				viewHolder.wallet_amount.setTextColor(getResources().getColor(amountColor.EXPEND.nativeInt));
			} else {
				viewHolder.wallet_amount.setTextColor(getResources().getColor(amountColor.INCOME.nativeInt));
			}
			return convertView;
		}
		
		// 复用
		class ViewHolder{
			TextView sourceType;
			TextView happenTime;
			TextView wallet_amount;
		}
		
	}
	
	/**
	 * 金额颜色枚举
	 * @author wangk
	 *
	 */
	private static enum amountColor{
		EXPEND(R.color.text_purple), // 支出
		INCOME(R.color.text_orange); // 收入
		private int nativeInt;
		private amountColor(int nativeInt) {
			this.nativeInt = nativeInt;
		}
	}
	
}
